Skip to content

Conversation

@dev-KingMaster
Copy link

@dev-KingMaster dev-KingMaster commented Nov 5, 2025

Summary

  • Removes PLW2901 ignore and fixes all instances where loop or context-manager variables were overwritten, improving readability and avoiding subtle bugs.
  • Why: Aligns with Ruff “no var overwrite” guidance; safer, clearer code; progresses Cleanup ruff ignores #3327.
  • Scope: Touches core modules (e.g., pypdf/_cmap.py, _doc_common.py, _page.py, _writer.py, _text_extraction/_layout_mode/_font.py, filters.py, generic/_files.py) and a few tests where the pattern occurred.
  • Approach: Introduces new local names (*_obj, *_val, operands_for_tm, etc.) instead of reassigning loop/context variables; no behavioral changes.
  • Config: Removes "PLW2901" from [tool.ruff.lint].ignore in pyproject.toml.
  • Validation: ruff check passes cleanly with PLW2901 enforced. No functional logic altered; tests adjusted only for variable naming where needed.

Contribution by Gittensor, learn more at https://gittensor.io/

@dev-KingMaster dev-KingMaster changed the title Ruff cleanup: enforce PLW2901 (no loop/context var overwrite) MAINT: Enforce PLW2901 (avoid loop/context var overwrite) (#3327) Nov 5, 2025
@codecov
Copy link

codecov bot commented Nov 5, 2025

Codecov Report

❌ Patch coverage is 98.38710% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 97.11%. Comparing base (1967d25) to head (66157a2).

Files with missing lines Patch % Lines
pypdf/_doc_common.py 92.30% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3513   +/-   ##
=======================================
  Coverage   97.11%   97.11%           
=======================================
  Files          57       57           
  Lines        9713     9720    +7     
  Branches     1758     1758           
=======================================
+ Hits         9433     9440    +7     
  Misses        168      168           
  Partials      112      112           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@stefan6419846 stefan6419846 changed the title MAINT: Enforce PLW2901 (avoid loop/context var overwrite) (#3327) MAINT: Enforce PLW2901 (avoid loop/context var overwrite) Nov 5, 2025
Copy link
Collaborator

@stefan6419846 stefan6419846 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR. I have left some remarks for specific changes and some general ones.

@dev-KingMaster
Copy link
Author

Hi @stefan6419846 I addressed all the comments.

while isinstance(d_font, IndirectObject):
d_font = d_font.get_object()
self.font_dictionary["/DescendantFonts"][d_font_idx] = d_font
if isinstance(d_font, IndirectObject):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do not need this check. Calling d_font.get_object() should always work.

pypdf/_page.py Outdated
v = ArrayObject(
[self._translate_value_inline_image(k, x) for x in v]
value_for_init = cast(
PdfObject,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ArrayObject is a PdfObject. If you want to type value_for_init, please use value_for_init: PdfObject = ... without the cast.

pypdf/_writer.py Outdated
# other cases will be updated through the for loop
annotation[NameObject(AA.AS)] = v
annotation[NameObject(FA.V)] = v
if flatten and appearance_stream_obj is not None:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where does this code originate from?

pypdf/_writer.py Outdated
if isinstance(value, PdfObject):
value = value.get_object()
args[NameObject(key)] = create_string_object(str(value))
value_obj = value.get_object() if isinstance(value, PdfObject) else value
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
value_obj = value.get_object() if isinstance(value, PdfObject) else value
value_object = value.get_object() if isinstance(value, PdfObject) else value

if isinstance(params, NullObject):
params = {}
for filter_name, params_untyped in zip(filters, decode_parms):
params_typed: Optional[DictionaryObject] = None
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not params directly to reduce the diff?

"""
)


Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is most likely wrong.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants